﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace The_LarX
{
    public class VisualScript_Node : IVisualScriptNode
    {
        protected static int Created;
        public int CreatedNumber;

        protected string descriptionName;
        public string DescriptionName { get { return descriptionName; } }

        public string IdentificationName;

        public VisualScript Script;

        public bool AllowConditions;

        public VSRBase Representation;
        public Type RepresentationType;

        public List<VisualScript_Node> Conditionals;

        public VisualScript_Node(VisualScript Script)
        {
            this.Script = Script;
            Script.Nodes.Add(this);
            Load();
        }
        public VisualScript_Node()
        {
            Load();
        }
        private void Load()
        {
            this.RepresentationType = typeof(VSRBase);
            CreatedNumber = Created;
            Created++;
            Conditionals = new List<VisualScript_Node>();
            IdentificationName = CreatedNumber + DescriptionName; 
        }

        public virtual bool AllConditionsAllow()
        {
            if (Conditionals.Count == 0)
                return true;
            else
            {
                int conditionsAllowed = 0;
                for (int i = 0; i < Conditionals.Count; i++)
                {
                    if (Conditionals[i].AllowConditions)
                        conditionsAllowed++;
                }
                if (conditionsAllowed == Conditionals.Count)
                    return true;
            }
            return false;
        }

        public virtual void Update()
        {
        }
        public virtual void Draw()
        {
        }

        public virtual void PlayModeFalse()
        {
        }

        public virtual void ReceiveInformation(NodeInformation Information)
        {
        }

        public virtual void StartRepresentation(VisualScriptEditor editor)
        {
            Representation = (VSRBase)Activator.CreateInstance(RepresentationType);
            Representation.Start(this, editor);
            if (Data != null)
            {
                Representation.DeserializeData(Data);
            }
        }

        private SerializeData Data;
        protected string CreatedNumberString { get { return CreatedNumber.ToString(); } }
        public virtual void SerializeData(SerializeData Data)
        {
            Data.AddData(CreatedNumber + "VisualNode.Identification", this.IdentificationName);
            Data.AddData(CreatedNumber + "VisualNode.Conditionals", NodesToStringWithNames(Conditionals));
            if (Representation != null)
            {
                Representation.SerializeData(Data);
            }
        }
        public static string NodesToStringWithNames(List<VisualScript_Node> nodes)
        {
            string listCha = "";
            for (int i = 0; i < nodes.Count; i++)
            {
                listCha += nodes[i].IdentificationName;
                if (i != nodes.Count - 1)
                    listCha += ";";
            }
            return listCha;
        }
        public static List<VisualScript_Node> StringWithNamesToNodes(string name, VisualScript Script)
        {
            List<VisualScript_Node> nodes = new List<VisualScript_Node>();
            string[] cond = name.Split(';');
            for (int i = 0; i < cond.Length; i++)
            {
                string ac = cond[i];
                for (int k = 0; k < Script.Nodes.Count; k++)
                {
                    if (Script.Nodes[k].IdentificationName == ac)
                    {
                        nodes.Add(Script.Nodes[k]);
                    }
                }
            }
            return nodes;
        }
        public virtual void DeserializeData(SerializeData Data)
        {
            this.IdentificationName = Data.GetData<string>(CreatedNumber + "VisualNode.Identification");
            this.Conditionals = StringWithNamesToNodes(Data.GetData<string>(CreatedNumber + "VisualNode.Conditionals"), this.Script);
            this.Data = Data;
        }
    }
}
